package com.ruoyi.purchase.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.purchase.vo.PurchaseOrderPageVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.PurchaseOrders;
import com.ruoyi.purchase.service.IPurchaseOrdersService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购订单Controller
 *
 * @author ruoyi
 * @date 2025-10-18
 */
@RestController
@RequestMapping("/purchase/purchaseOrders")
public class PurchaseOrdersController extends BaseController
{
    @Autowired
    private IPurchaseOrdersService purchaseOrdersService;

    /**
     * 查询采购订单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchaseOrders:list')")
    @GetMapping("/list")
    public TableDataInfo list(PurchaseOrderPageVO dto)
    {
        startPage();
        List<PurchaseOrderPageVO> list = purchaseOrdersService.getPurchaseOrderPageList(dto);
//        System.out.println("查询返回的页面的信息"+list);
        return getDataTable(list);
    }
    /**
     * 获取采购订单详情
     */
    @GetMapping("/{poId}/{itemId}")
    public AjaxResult getInfo(@PathVariable String poId,@PathVariable String itemId) {
        PurchaseOrders  purchaseOrders  = purchaseOrdersService.getDetailByPoIdAndItemId(poId,itemId);
        System.out.println("返回给前端修改页面的数据"+purchaseOrders);
        return AjaxResult.success(purchaseOrders);
    }

    /**
     * 获取采购订单详情页数据
     */
    @GetMapping("/detailInfo/{poId}/{itemId}")
    public AjaxResult detailInfo(@PathVariable String poId,@PathVariable String itemId) {
//        System.out.println("采购详情传过来的采购id存货"+poId+itemId);
        PurchaseOrderPageVO  purchaseOrders  = purchaseOrdersService.getDetailInfoByPoIdAndItemId(poId,itemId);
//        System.out.println("返回给前端详情的数据"+purchaseOrders);
        return AjaxResult.success(purchaseOrders);
    }

    /**
     * 获取新采购订单号
     */
    @GetMapping("/generatePoId")
    public AjaxResult generatePoId() {
        String poId = purchaseOrdersService.generatePoId();
        System.out.println(poId);
        return AjaxResult.success(poId);
    }










    /**
     * 导出采购订单列表
     */
//    @PreAuthorize("@ss.hasPermi('purchase:purchaseOrders:export')")
//    @Log(title = "采购订单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, PurchaseOrders purchaseOrders)
//    {
//        List<PurchaseOrders> list = purchaseOrdersService.selectPurchaseOrdersList(purchaseOrders);
//        ExcelUtil<PurchaseOrders> util = new ExcelUtil<PurchaseOrders>(PurchaseOrders.class);
//        util.exportExcel(response, list, "采购订单数据");
//    }

    /**
     * 获取采购订单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('purchase:purchaseOrders:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") String id)
//    {
//        return success(purchaseOrdersService.selectPurchaseOrdersById(id));
//    }

    /**
     * 新增采购订单
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchaseOrders:add')")
    @Log(title = "采购订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurchaseOrders purchaseOrders)
    {
        // 设置创建时间和更新时间
        purchaseOrders.setCreatedAt(new Date());
        purchaseOrders.setUpdatedAt(new Date());
//        System.out.println("新增采购订单"+purchaseOrders);
        int result = purchaseOrdersService.insertPurchaseOrderWithDetails(purchaseOrders);
        return result > 0 ? AjaxResult.success() : AjaxResult.error("新增采购订单失败");
    }

    /**
     * 修改采购订单
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchaseOrders:edit')")
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PurchaseOrders purchaseOrders)
    {
        System.out.println("修改传过来的"+purchaseOrders);
        return toAjax(purchaseOrdersService.updatePurchaseOrderWithDetails(purchaseOrders));
    }

    /**
     * 删除采购订单
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchaseOrders:remove')")
    @Log(title = "采购订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long  ids)
    {
        System.out.println("需要删除id"+ids);
        return toAjax(purchaseOrdersService.deletePurchaseOrdersById(ids));
    }

    /**
     * 批量删除采购订单
     */
    @PreAuthorize("@ss.hasPermi('purchase:purchaseOrders:remove')")
    @Log(title = "采购订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch/{ids}")
    public AjaxResult remove(@PathVariable Long [] ids) {
        System.out.println("需要删除数组id"+ids);
        return toAjax(purchaseOrdersService.deletePurchaseOrdersByIds(ids));
    }
}
